package com.mulanweb.api.module.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;
import com.mulanweb.utils.WeiXinUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/8.
 */
public class TestController extends Controller{


    public void getUser(){
        String para = getPara("ToUserName ");
        if(para!=null){
            
        }
    }

    public void test(){
        String paras = getRequest().getQueryString() ;
        String[] paras1 = paras.split("&") ;
        Map<String,Object> para = new HashMap<String,Object>();
        for(String parameter : paras1){
            String[] parameter1 =parameter.split("=") ;
            para.put(parameter1[0].toString(),parameter1[1]);
        }
        String timestamp = para.get("timestamp")!=null ? para.get("timestamp").toString() : null ;
        String nonce = para.get("nonce")!=null ? para.get("nonce").toString() : null ;
        String echostr = para.get("echostr")!=null ? para.get("echostr").toString() : null ;
        String signature = para.get("signature")!=null ? para.get("signature").toString() : null ;

        String[] result= {"zenghf@521",nonce,timestamp};

        Arrays.sort(result);

        StringBuffer sb = new StringBuffer() ;

        for(String tt : result){
            sb.append(tt) ;
        }
        String sha1 = HashKit.sha1(sb.toString()) ;
        if(sha1.equals(signature)){
            renderText(echostr);
        }
    }

    public static String loadJson (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL urlObject = new URL(url) ;
            URLConnection uc = urlObject.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    public void saveUser(){
    }
}
